package com.wunu.smartlock1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wunu.smartlock1.R;
import com.wunu.smartlock1.bluetooth.WunuBlueManager;
import com.wunu.smartlock1.bluetooth.command.CheckBindCommand;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockManageApi;
import com.wunu.smartlock1.net.param.BindLockParam;
import com.wunu.smartlock1.net.response.ApiNormalResponse;
import com.wunu.smartlock1.net.response.BindLockResult;
import com.wunu.smartlock1.util.CommonUtil;

import java.util.Iterator;
import java.util.Map;

import retrofit2.Call;


/**
 * 扫描搜索到设备后点击绑定
 * Created by admin on 2018/4/21.
 */

public class LockBindActivity_thread_bak extends AppCompatActivity{
    Button bindButton;
    BindLockParam param;

    TextView lockMac;
    TextView lockName;
    TextView lockNote;
    ImageView lockImage;
    Intent intent = new Intent();
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+msg);
            System.out.println("obj->"+msg.getData());

            if (msg == null || msg.obj == null){
                Toast.makeText(LockBindActivity_thread_bak.this, "绑定失败", Toast.LENGTH_SHORT).show();
            }
            //返回的结果: BindLockBean{lockId=78, managerId=90}
            Toast.makeText(LockBindActivity_thread_bak.this, "绑定成功"+msg.obj, Toast.LENGTH_SHORT).show();
        }
    };
    Handler bluethhandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+CommonUtil.map);
            System.out.println(CommonUtil.map);
            Iterator<String> iterator = CommonUtil.map.keySet().iterator();
            while (iterator.hasNext()){
                String key = iterator.next();
                System.out.println(key);
                System.out.println(CommonUtil.map.get(key));
            }
            System.out.println("msg-----------------end->:"+CommonUtil.map);
        }
    };
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.smartlock_activity_bindlock);
        bindButton = findViewById(R.id.bindButton);
        lockMac = findViewById(R.id.lockMac);
        lockName = findViewById(R.id.lockName);
        lockNote = findViewById(R.id.lockNote);
        lockImage = findViewById(R.id.lockImage);
        param = new BindLockParam.Builder()
                .setBasecode(648845)
                .setLockModel(1)//锁类型
                .setLockMac(lockMac.getText().toString())//DF:ED:67:87:01:32
                .setLockName(lockName.getText().toString())
                .setLockNote(lockNote.getText().toString())//锁申请公告
                .setLockImageUrl(lockImage.getTransitionName())//./../../images/lock_3.png
                .build();


        new Thread(){
            @Override
            public  void run(){
                System.out.println("蓝牙名称:"+ CommonUtil.device.getName()+"\tmac:"+CommonUtil.device.getMac());
                CheckBindCommand cmd = new CheckBindCommand();
                Map map = cmd.decode(cmd.payload());
                System.out.println(map);
                WunuBlueManager manager = new WunuBlueManager();
                manager.setCommand(cmd);
                manager.connectDevice(CommonUtil.device);


                try {
                    Thread.sleep(10000);
                } catch(Exception e){

                }
                // 发送消息，把服务器返回的本文弹出吐司显示
                Message msg = bluethhandler.obtainMessage();
                msg.obj = manager.getMap();
                System.out.println("蓝牙通信结果:"+msg.obj);
                bluethhandler.sendMessage(msg);
                System.out.println("-------------------------------!>");
            }
        }.start();

       bindButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               System.out.println("点击了绑定按钮:");
               final LockManageApi service = NetComponent.getRetrofit().create(LockManageApi.class);
               //开线程加载数据
               new Thread() {
                   @Override
                   public void run() {
                       super.run();
                       try{
                           Call<ApiNormalResponse<BindLockResult>> call = service.bindLock(param);
                           ApiNormalResponse<BindLockResult> resp = call.execute().body();
                           System.out.println(resp.getData().getData().toString());

                           // 发送消息，把服务器返回的本文弹出吐司显示
                           Message msg = handler.obtainMessage();
                           if(resp == null || resp.getData() == null)msg.obj = null;
                           else msg.obj = resp.getData().getData();
                           System.out.println("绑定结果:"+msg.obj);
                           handler.sendMessage(msg);
                       } catch (Exception e) {
                           System.out.println("锁绑定异常:" + e);
                       }
                   }
               }.start();
           }
       });
    }
    public abstract  class  Tests<String,Object> {
        public abstract void calls(Map<String,Object> map);
        //public abstract Map<String,Object> calls();
    }
}
